﻿namespace iFinance.Modules.Authorization.ViewModels
{
    using System.ComponentModel.DataAnnotations;
    using System.Windows.Input;
    using iFinance.IDAL;
    using iFinance.Infrastructure;
    using iFinance.Infrastructure.Events;
    using iFinance.Infrastructure.ViewModels;
    using Microsoft.Practices.Prism.Commands;
    using Microsoft.Practices.Prism.Events;
    using Microsoft.Practices.Unity;

    /// <summary>
    /// 登录ViewModel
    /// </summary>
    public class LoginViewModel : ValidationNotificationObject
    {
        private readonly IEventAggregator eventAggregator = null;

        public LoginViewModel(IEventAggregator eventAggregator)
        {
            this.eventAggregator = eventAggregator;
            this.LoginCommand = new DelegateCommand(this.Login);
        }

        #region Property

        private string userID = string.Empty;

        [Display(Name = "账户")]
        public string UserID
        {
            get { return this.userID; }
            set
            {
                if (this.userID != value)
                {
                    this.userID = value;
                    RaisePropertyChanged(() => this.UserID);
                }
            }
        }

        private string password = string.Empty;

        [Display(Name = "密码")]
        public string Password
        {
            get { return this.password; }
            set
            {
                if (this.password != value)
                {
                    this.password = value;
                    RaisePropertyChanged(() => this.Password);
                }
            }
        }

        #endregion

        #region Command

        #region LoginCommand

        private ICommand loginCommand = null;

        public ICommand LoginCommand
        {
            get { return this.loginCommand; }
            set
            {
                if (loginCommand != value)
                {
                    this.loginCommand = value;
                    RaisePropertyChanged(() => this.LoginCommand);
                }
            }
        }

        #endregion

        #endregion

        #region Method

        /// <summary>
        /// 注册用户
        /// </summary>
        public void Login()
        {
            string msg = Resources.QueryFailed;
            var dao = UnityHelper.Container.Resolve<IUserDAO>();
            if (dao != null)
            {
                var user = dao.QueryUserByID(this.UserID);
                if (user != null)
                {
                    if (user.Password.Equals(this.Password))
                    {
                        //登录成功
                        //msg = Resources.LoginSuccess;
                        RemoveError(() => this.Password, Resources.WrongPassword);
                        this.eventAggregator.GetEvent<LoginSuccessEvent>().Publish(this.UserID);
                    }
                    else
                    {
                        //msg = Resources.WrongPassword;
                        AddError(() => this.Password, Resources.WrongPassword);
                    }
                    RemoveError(() => this.UserID, Resources.UserIDNotExist);
                }
                else
                {
                    AddError(() => this.UserID, Resources.UserIDNotExist);
                    //msg = Resources.UserIDNotExist;
                }
            }
            //MessageBox.Show(msg);
        }

        #endregion
    }
}
